const Service = require('egg').Service;
// const { Op } = require('sequelize');

class CategoriesService extends Service {
  // 查询分类
  async findAll({ pageIndex = 1, pageSize = 3 }) {
    const { ctx } = this;
    const offset = (pageIndex - 1) * pageSize;
    return await ctx.model.Categories.findAndCountAll({
      offset: Number(offset),
      limit: Number(pageSize),
    });
  }

  // 添加
  async add(categories) {
    const { ctx } = this;
    return await ctx.model.Categories.create(categories);
  }

  // 删除
  async remove(id) {
    const { ctx } = this;
    const affectedRows = await ctx.model.Categories.destroy({
      where: { id },
    });
    return !!affectedRows;
  }

  // 修改
  async modify(categories, id) {
    const { ctx } = this;
    const [ affectedRows ] = await ctx.model.Categories.update(categories, {
      where: { id },
    });
    return !!affectedRows;
  }

  // 查询单条数据
  async findById(id) {
    const { ctx } = this;
    return await ctx.model.Categories.findByPk(id);
  }
}
module.exports = CategoriesService;
